# coding=utf-8
import datetime

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from matplotlib.dates import DateFormatter, WeekdayLocator, MONDAY
from sklearn import preprocessing

pd.set_option('display.height', 1000)
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)
plt.rcParams.update({'figure.autolayout': True})

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

na_black_friday = ['2014-11-28',
                   '2015-11-27',
                   '2016-11-25',
                   '2017-11-24',
                   '2018-11-30']
na_labor_day = ['2014-09-01',
                '2015-09-07',
                '2016-09-05',
                '2017-09-04',
                '2018-09-03']
emea_kaizhaijie = ['2014-07-29',
                   '2015-07-18',
                   '2016-07-05',
                   '2017-06-26',
                   '2018-06-15']

emea_guerbangjie = ['2014-10-04',
                    '2015-09-24',
                    '2016-09-13',
                    '2017-09-02',
                    '2018-08-22']
prc_double11 = ['2014-11-11',
                '2015-11-11',
                '2016-11-11',
                '2017-11-11',
                '2018-11-11']
prc_chinesenewyear = ['2014-02-01',
                      '2015-02-18',
                      '2016-02-07',
                      '2017-01-28',
                      '2018-02-16']
india_diwali_start = ['2014-10-23',
                      '2015-11-11',
                      '2016-10-30',
                      '2017-10-18',
                      '2018-11-06']
india_diwali_end = ['2014-10-27',
                    '2015-11-15',
                    '2016-11-03',
                    '2017-10-22',
                    '2018-11-10']
christmas = ['2014-12-25',
             '2015-12-25',
             '2016-12-25',
             '2017-12-25',
             '2018-12-25']

# --
# ----
sub_geo = 'RUSSIA/CIS'  # 'NA'
# geo = 'NA'
business_type = 'Consumer'  # 'Commercial'
# year_range = 2014
holiday_black = india_diwali_start
holiday_red = india_diwali_end
# ------------------- start reading -------------------------------------------
df = pd.read_csv('F:/!Python/helloworld2/feature_data_20171113_1_2.csv')
values = {'geo': 'NA', 'sub_geo': 'NA', 'date': '1989-01-01', 'original_quantity': 1}
df = df.fillna(value=values)

df_g = pd.read_csv('data/family_group.csv')
df_g = df_g[['family_desc', 'PORTFOLIO', 'LEVEL', 'CATEGORY']]
df_m = df.merge(df_g, 'left', 'family_desc')
df = df_m

df_h = pd.read_csv('data/holiday_all_utf8.csv')
values_h = {'holiday': 'undefined', 'subgeo': 'NA', 'year': 1989,
            'rsd': datetime.datetime.now().replace(1989, 1, 1, 0, 0, 0, 0)}
df_h = df_h.fillna(value=values_h)
# print(df_h)

# -- 分类 --
# df = df[df['geo'] == geo]


df = df[df['sub_geo'] == sub_geo]
df = df[df['business_type'] == business_type]
# df = df[(df['CATEGORY'] == "Traditional") |
#         (df['CATEGORY'] == "Ultraslim") |
#         (df['CATEGORY'] == "Gaming") |
#         (df['CATEGORY'] == "Tablet")]  # |
#  (df['CATEGORY'] == np.nan)]


russian_impulse_list = ['V110-15ISK',
                        'V310-15IKB',
                        'V310-15ISK',
                        '310-15ASR',
                        'AIO 910-27ISH',
                        '510-15IKL',
                        '510-15ABR',
                        '110-14IBR',
                        '110-15IBR',
                        '110-15IBR(BRAZIL)']
for fmly_ele in russian_impulse_list:
    df = df[(df['family_desc'] != fmly_ele)]
japan_impulse_list = ['320-15IAP', '320-15IAP(BRAZIL)',
                      '320-15IKBN(BRAZIL)', '320-15IKBRN']  #, '320-15ISK'
# for fmly_ele in japan_impulse_list:
#     df = df[(df['family_desc'] != fmly_ele)]

# df = df[(df['CATEGORY'] != "Education")]  &
# (df['CATEGORY'] != "Covertible") &
# df = df[(df['CATEGORY'] != "ZHAOYANG")]
# (df['CATEGORY'] != "Tailor Offering")
# ]   |
#  (df['CATEGORY'] == np.nan)]

df = df[['sub_geo', 'family_desc', 'geo', 'group_id', 'date', 'original_quantity', 'business_type']]
assert isinstance(df, pd.DataFrame)
df['new_col'] = df['sub_geo'] + '@' + df['group_id'] + '@' + df['family_desc']
df = df[['new_col', 'date', 'original_quantity']]
df_non_scale = df.copy()
df = df.drop_duplicates()
df = df.pivot('date', 'new_col', 'original_quantity')  # 生成透视图
df = df.fillna(0)

# df = df.T  # TODO:fawefawefawefawfawefawef
x = df.values  # returns aa numpy array
# ---------- 进行归一化处理 ----------------------
# ---- 准备热力图数据 ----
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
df_c = df.columns
df = pd.DataFrame(x_scaled, index=df.index.values, columns=df.columns)
# df = df.T  # TODO:afwoiejfioawjefoijawefoijawf
# ---- 准备归一化图数据
df_s = df.stack()
df_ss = df_s.copy()

print(df[df > 0.5])
# print(df_s[(df_s['quantity'] > 0.5)])  # (df['date'] < "2016-04-18") &
# exit(0)

df_s = df_s.groupby(df_s.index.get_level_values(0)).sum()
df_s_index_list_1 = list(map(lambda xx: datetime.datetime.strptime(xx, '%Y-%m-%d'), df_s.index.values.tolist()))
df_s_values_1 = df_s.values.tolist()

# ---- 生成同一年的合并对比数据 ----
# step 1: 对归一化数据进行铺平
assert isinstance(df_ss, pd.Series)
index_v = df_ss.index.values.tolist()
value = df_ss.values.tolist()
value_list = []
for i in range(0, len(value)):
    # print(datetime.datetime.strptime(index_v[i][0], '%Y-%m-%d'))
    content = [datetime.datetime.strptime(index_v[i][0], '%Y-%m-%d'),
               index_v[i][1],
               value[i]]
    # print(content)
    value_list.append(content)
df_ss = pd.DataFrame(value_list, columns=['date', 'new_col', 'quantity'])
# ---- 对归一化的数据进行铺平整理完毕 ----

df_tmp = df_ss

# df1 = df_tmp[(df_tmp.date >= (str(year_range)+"-01-01")) &
#              (df_tmp.date < (str(year_range+1)+"-01-01"))]
# df2 = df_tmp[(df_tmp.date >= (str(year_range+1)+"-01-01")) &
#              (df_tmp.date < (str(year_range+2)+"-01-01"))]
#
# df1_tmp = df1.copy()
# df1['date'] = df1_tmp['date'] + datetime.timedelta(days=364)
#
# df_m = df1.merge(df2, 'outer', ['date', 'new_col'])
# df_m = df_m.fillna(0)
# df_m['quantity'] = df_m['quantity_x'] + df_m['quantity_y']
df_m = df_ss
# ------------- 对每个date进行sum ---------------------
df_m = df_m.groupby(df_m['date']).sum()
# print(df_m)
# print(df_m.dtypes)

# exit(0)
df_s_index_list = df_m.index.values.tolist()
df_s_index_list = list(map(lambda xx: datetime.datetime.fromtimestamp(xx / 1000000000), df_s_index_list))
# print(df_s_index_list)
# exit(0)
df_s_values = df_m['quantity'].values.tolist()
# print(df_s_index_list)
# exit(0)
# -------------- 进行图像绘制 -----------------
# ------------- 绘制归一化图 ----------------
f, (ax2, ax1) = plt.subplots(figsize=(10, 8), nrows=2)
ax1.plot(df_s_index_list, df_s_values)
# for day in holiday_black:  # 添加黑色节日
#     datetime_day = datetime.datetime.strptime(day, '%Y-%m-%d')
#     ax1.axvline(datetime_day, label="1", color='black', linestyle="--")  # hold=None
# for day in holiday_red:  # 添加红色节日
#     datetime_day = datetime.datetime.strptime(day, '%Y-%m-%d')
#     ax1.axvline(datetime_day, label="1", color='red', linestyle="--")  # hold=None
# for day in christmas:  # 添加红色节日
#     datetime_day = datetime.datetime.strptime(day, '%Y-%m-%d')
#     ax1.axvline(datetime_day, label="1", color='orange', linestyle="--")  # hold=None

df_h = df_h[df_h['subgeo'] == sub_geo]  # 筛选出当前sub_geo的节日，进行节日线的绘制
assert isinstance(df_h, pd.DataFrame)
df_h_g = df_h.groupby('holiday').count()
holiday_list_from_subgeo = df_h_g.index.values.tolist()
color_list = ['black', 'red', 'blue', 'orange', 'pink',
              'green', 'purple', 'brown', 'gray', 'magenta',
              'black', 'red', 'blue', 'orange', 'pink']
title = ""
i = 0
for holiday_single in holiday_list_from_subgeo:
    df_h_s = df_h[df_h['holiday'] == holiday_single]
    # print(df_h_s)
    h_list = df_h_s['rsd'].values.tolist()
    for day in h_list:  # 添加红色节日
        datetime_day = datetime.datetime.strptime(day, '%Y/%m/%d')
        ax1.axvline(datetime_day, label="1", color=color_list[i], linestyle="--")  # hold=None
    title += holiday_single + "-" + color_list[i] + "; "
    i += 1

# exit(0)

ax1_2 = ax1.twinx()  # 创建第二个坐标轴
# print(df_non_scale)
df_group = df_non_scale.groupby(df_non_scale['date']).sum()
print(df_group)
x = df_group.index.values.tolist()
print(x)
x = list(map(lambda xx: datetime.datetime.strptime(xx, '%Y-%m-%d'), x))
print(x)
# exit(0)
ax1_2.plot(x, df_group['original_quantity'], 'y-')
# exit(0)

ax1.axhline(0, label="1", color='green', linestyle="--")
yearsFmt = DateFormatter('%Y-%m-%d')
ax1.xaxis.set_major_locator(WeekdayLocator(MONDAY, interval=4))
ax1.xaxis.set_major_formatter(yearsFmt)  # 设置主要时间显示格式(日期类型的格式化)
ax1.xaxis.set_minor_locator(WeekdayLocator(MONDAY))  # 设置次要时间间隔!!!!! 成功
df_s_index_list_1.sort()
min_date = df_s_index_list_1[0]
max_date = df_s_index_list_1[len(df_s_index_list_1) - 1]
ax1.set(xlim=[min_date, max_date])
labels = ax1.get_xticklabels()
plt.setp(labels, rotation=45, horizontalalignment='right')  # 标签是竖着排、还是横着排、还是斜着排

# ------------ 绘制热力图 -------------------
df = df.T
sns.heatmap(df, center=0, ax=ax2, cbar=False)
labels = ax2.get_xticklabels()
plt.setp(labels, rotation=45, horizontalalignment='right')
ax2.set(xlabel='datetime', ylabel='family_subgeo',
        title=sub_geo + ':  ' + title)
fig = plt.figure(1)  # fig参数通过plt得到
fig.set_size_inches(19, 9)  # 可以大致认为保存的图像为1900像素x1000像素
# print()
plt.show()
